package com.wanchuang.qhbb.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.wanchuang.qhbb.R;

import kale.ui.view.dialog.BaseCustomDialog;
import kale.ui.view.dialog.BaseEasyDialog;

/**
 * Created by AndroidIntexh1 on 2018/9/25.
 */

public class EditRateDialog extends BaseCustomDialog {

    private EditText etv_content;
    private TextView sub;
    private TextView cancle;
    private static final String KEY_INPUT_TEXT = "KEY_INPUT_TEXT";
    private static final String KEY_TITLE_TEXT = "KEY_TITLE_TEXT";
    private static final String KEY_MESSAGE_TEXT = "KEY_MESSAGE_TEXT";
    public OnDialogClickListener dialogListener;
    private TextView title;
    private String titles;
    private TextView tvZero;
    private TextView tvDan;

    /**
     * 自定义builder来增加一些参数，记得要继承自BaseEasyDialog.Builder
     */
    public static class Builder extends BaseEasyDialog.Builder<Builder> {

        private Bundle bundle = new Bundle();
        private OnDialogClickListener listener;

        public Builder(@NonNull Context context) {
            super(context);
        }

        public Builder setTitleText(CharSequence text) {
            bundle.putCharSequence(KEY_TITLE_TEXT, text);
            return this;
        }

        public Builder setMessageText(CharSequence text) {
            bundle.putCharSequence(KEY_MESSAGE_TEXT, text);
            return this;
        }


        public Builder setOnDialogClickListener(OnDialogClickListener lis) {
            listener = lis;
            return this;
        }

        @NonNull
        @Override
        protected EditRateDialog createDialog() {
            EditRateDialog dialog = new EditRateDialog();
            dialog.setArguments(bundle);
            dialog.dialogListener = listener;
            return dialog;
        }
    }


    @Override
    protected int getLayoutResId() {
        return R.layout.dialog_rate_input;
    }

    @Override
    protected void bindViews(View view) {
        etv_content = findView(R.id.etv_content);
        sub = findView(R.id.btn_sure);
        cancle = findView(R.id.btn_cancel);
        title = findView(R.id.tv_title);
        tvZero = findView(R.id.tv_zero);
        tvDan = findView(R.id.tv_dan);
    }

    @Override
    protected void setViews() {
        setBackground();
        setLayout();

        cancle.setOnClickListener(view -> dismiss());
        titles = getArguments().getString(KEY_TITLE_TEXT);
        title.setText(titles);

        if (getArguments().get(KEY_MESSAGE_TEXT).equals("1"))
        {
            tvZero.setVisibility(View.INVISIBLE);
            tvDan.setText("元");
        }else
        {
            tvZero.setVisibility(View.VISIBLE);
            tvDan.setText("%");
        }
        sub.setOnClickListener(view -> {
            String s = etv_content.getText().toString();
            if (TextUtils.isEmpty(s)) {
                Toast.makeText(getContext(),"数字不能为空",Toast.LENGTH_SHORT).show();
            } else {

                if (getArguments().get(KEY_MESSAGE_TEXT).equals("1"))
                {

                    dialogListener.onClick(view, s);
                }else
                {
                    dialogListener.onClick(view, "0."+s);
                }
                dismiss();

            }

        });
        etv_content.setFocusable(true);
        etv_content.setFocusableInTouchMode(true);
        etv_content.requestFocus();
    }

    /**
     * 强制取消背景，保持有透明
     */
    private void setBackground() {
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable()); // 去除dialog的背景，即透明
//        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0xffffffff)); // 设置白色背景
//        getDialog().getWindow().setBackgroundDrawableResource(R.drawable.dialog_white_bg); // 设置背景
    }


    /**
     * 也可通过setLayout来设置：
     * getDialog().getWindow().setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height);
     */
    private void setLayout() {
        Window window = getDialog().getWindow();
        final WindowManager.LayoutParams lp = window.getAttributes();

        // 强制宽高
//        int padding = getResources().getDimensionPixelOffset(R.dimen.dialog_padding);
        lp.width = getScreenWidth(getActivity());
//        lp.height = getResources().getDimensionPixelOffset(R.dimen.dialog_height);

        lp.gravity = Gravity.CENTER; // 设置展示的位置
//        lp.y = getResources().getDimensionPixelOffset(R.dimen.join_shop_dialog_margin);
        window.setAttributes(lp);
    }

    public static int getScreenWidth(Activity activity) {
        final DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        etv_content = null;
    }

    /**
     * 监听输入框内容
     */
    public interface OnDialogClickListener {
        /**
         * Called when a view has been clicked.
         *
         * @param v The view that was clicked.
         */
        void onClick(View v, String inputText);
    }

    @Override
    public void onResume() {
        super.onResume();
        (new Handler()).postDelayed(new Runnable() {
            public void run() {
                InputMethodManager inManager = (InputMethodManager) etv_content.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }, 200);
    }
}
